package com.brayan.webapp.controller;

import java.io.IOException;
import java.io.OutputStream;

import javax.ejb.EJB;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;

import com.brayan.webapp.model.UploadedImage;
import com.brayan.webapp.repository.ImageRepository;

/**
 * This Controller helps to retrieve on-the-fly generated images from a
 * persistence source.
 * 
 * @author Brayan
 * 
 */
@ManagedBean
@ApplicationScoped
public class ImageController {
	@EJB
	private ImageRepository imageRepository;

	public void paintHeaderImage(OutputStream stream, Object object)
			throws IOException {
		Long imageId = (Long) object;
		UploadedImage ui = imageRepository.getImageById(imageId);
		stream.write(ui.getData());
		stream.close();
	}
}
